from django.shortcuts import render
from ..renew.models import Tv, TvList, Movie, MovieList, Cartoon, CartoonList
import re


def tv_play_first(request, false_cid):
    true_cid = Tv.objects.get(id__exact=false_cid).cid
    play_lists = TvList.objects.filter(cid__exact=true_cid).order_by('id')
    title = play_lists[0].play_title
    href = play_lists[0].href
    next_set_data = play_lists[1].id
    context = {
        "play": "/play/tv/second",
        "class": "play_a_1",
        "false_cid": false_cid,
        "play_lists": play_lists,
        "title": title,
        "href": href,
        "next_set": next_set_data,
    }
    response = render(request, "play.html", context=context)
    response.set_cookie('play_tv_title', title.encode('utf-8').decode('iso-8859-1'))
    response.set_cookie('play_tv_href', '/play/tv/first/' + false_cid)
    return response


def tv_play_second(request, false_cid, false_vid):
    true_cid = Tv.objects.get(id__exact=false_cid).cid
    play_lists = TvList.objects.filter(cid__exact=true_cid).order_by('id')
    play_data = TvList.objects.get(id__exact=false_vid)
    title = play_data.play_title
    href = play_data.href
    next_set = re.search("[0-9]+", title)[0]
    previous_episode_data = None
    if int(next_set) > 1:
        previous_episode = int(next_set) - 2
        previous_episode_data = play_lists[int(previous_episode)].id
    try:
        next_set_data = play_lists[int(next_set)].id
    except IndexError:
        next_set_data = None
    context = {
        "play": "/play/tv/second",
        "class": "play_a_1",
        "false_cid": false_cid,
        "play_lists": play_lists,
        "title": title,
        "href": href,
        "previous_episode": previous_episode_data,
        "next_set": next_set_data,
    }
    response = render(request, "play.html", context=context)
    response.set_cookie('play_tv_title', title.encode('utf-8').decode('iso-8859-1'))
    response.set_cookie('play_tv_href', '/play/tv/second/' + false_cid + '/' + false_vid)
    return response


def movie_play_first(request, false_cid):
    true_cid = Movie.objects.get(id__exact=false_cid).cid
    play_lists = MovieList.objects.filter(cid__exact=true_cid).order_by('id')
    title = play_lists[0].play_title
    href = play_lists[0].href
    context = {
        "play": "/play/movie/second",
        "class": "play_a_2",
        "false_cid": false_cid,
        "play_lists": play_lists,
        "title": title,
        "href": href
    }
    response = render(request, "play.html", context=context)
    response.set_cookie('play_movie_title', title.encode('utf-8').decode('iso-8859-1'))
    response.set_cookie('play_movie_href', '/play/movie/first/' + false_cid)
    return response


def movie_play_second(request, false_cid, false_vid):
    true_cid = Movie.objects.get(id__exact=false_cid).cid
    play_lists = MovieList.objects.filter(cid__exact=true_cid).order_by('id')
    play_data = MovieList.objects.get(id__exact=false_vid)
    title = play_data.play_title
    href = play_data.href
    context = {
        "play": "/play/movie/second",
        "class": "play_a_2",
        "false_cid": false_cid,
        "play_lists": play_lists,
        "title": title,
        "href": href
    }
    response = render(request, "play.html", context=context)
    response.set_cookie('play_movie_title', title.encode('utf-8').decode('iso-8859-1'))
    response.set_cookie('play_movie_href', '/play/movie/second/' + false_cid + '/' + false_vid)
    return response


def cartoon_play_first(request, false_cid):
    true_cid = Cartoon.objects.get(id__exact=false_cid).cid
    play_lists = CartoonList.objects.filter(cid__exact=true_cid).order_by('id')
    title = play_lists[0].play_title
    href = play_lists[0].href
    next_set_data = play_lists[1].id
    context = {
        "play": "/play/cartoon/second",
        "class": "play_a_1",
        "false_cid": false_cid,
        "play_lists": play_lists,
        "title": title,
        "href": href,
        "next_set": next_set_data,
    }
    response = render(request, "play.html", context=context)
    response.set_cookie('play_cartoon_title', title.encode('utf-8').decode('iso-8859-1'))
    response.set_cookie('play_cartoon_href', '/play/cartoon/first/' + false_cid)
    return response


def cartoon_play_second(request, false_cid, false_vid):
    true_cid = Cartoon.objects.get(id__exact=false_cid).cid
    play_lists = CartoonList.objects.filter(cid__exact=true_cid).order_by('id')
    play_data = CartoonList.objects.get(id__exact=false_vid)
    title = play_data.play_title
    href = play_data.href
    next_set = re.search("[0-9]+", title)[0]
    previous_episode_data = None
    if int(next_set) > 1:
        previous_episode = int(next_set) - 2
        previous_episode_data = play_lists[int(previous_episode)].id
    try:
        next_set_data = play_lists[int(next_set)].id
    except IndexError:
        next_set_data = None
    context = {
        "play": "/play/cartoon/second",
        "class": "play_a_1",
        "false_cid": false_cid,
        "play_lists": play_lists,
        "title": title,
        "href": href,
        "previous_episode": previous_episode_data,
        "next_set": next_set_data,
    }
    response = render(request, "play.html", context=context)
    response.set_cookie('play_cartoon_title', title.encode('utf-8').decode('iso-8859-1'))
    response.set_cookie('play_cartoon_href', '/play/cartoon/second/' + false_cid + '/' + false_vid)
    return response
